#include "000 basic.h"
// ## 2023/11/03 # 黑店小小二 # 自动内政函数 ##
namespace auto_inner
{
	const float 武将已行动折扣 = 0.2f;
	const float 武将不在城折扣 = 0.3f;

	pk::random randint(pk::rand());

	// 是否自动内政下进行的内政行为
	bool is_auto_affairs(pk::building@ building, pk::list<pk::person@> actor_list)
	{
		return ch::get_auto_affairs_status(building) and actor_list.count == 1;
	}

	/*
		获取内政对应的优势特技跟生效五维
		@params inner_type {INT} 内政类型
		@return first，second {INT_INT} first-内政生效五维  second-优势特技
	*/
	pk::int_int get_inner_type_stat_skill(int inner_type)
	{
		/* 
			const int 内政类型_征兵 = 0;
			const int 内政类型_训练 = 1;
			const int 内政类型_巡查 = 2;
			const int 内政类型_兵装 = 3;
			const int 内政类型_育马 = 4;
			const int 内政类型_攻具 = 5;
			const int 内政类型_造船 = 6;
		*/

		int param1;
		int param2;
#if param_set
		switch (inner_type)
		{
			case 内政类型_征兵:
				param1 = pk::get_recruit_param().stat;//int(pk::core["recruit.stat"]);
				param2 = pk::get_recruit_param().skill; //int(pk::core["recruit.skill"]);
				break;

			case 内政自动化_训练:
				param1 = pk::get_drill_param().stat;//int(pk::core["train.stat"]);
				param2 = 特技_调练;
				break;

			case 内政自动化_巡查:
				param1 = pk::get_patrol_param().stat;//int(pk::core["inspection.stat"]);
				param2 = 特技_巡查;
				break;

			case 内政自动化_兵装:
				param1 = pk::get_produce_param().stat;//int(pk::core["weapon_produce.stat"]);
				param2 = pk::get_produce_param().smith_skill; //int(pk::core["weapon_produce.smith_skill"]);
				break;
			
			case 内政自动化_育马:
				param1 = pk::get_produce_param().stat;//int(pk::core["weapon_produce.stat"]);
				param2 = pk::get_produce_param().stable_skill; //int(pk::core["weapon_produce.stable_skill"]);
				break;

			case 内政自动化_攻具:
				param1 = pk::get_produce_param().stat;//int(pk::core["weapon_produce.stat"]);
				param2 = pk::get_produce_param().workshop_skill; //int(pk::core["weapon_produce.workshop_skill"]);
				break;

			case 内政自动化_造船:
				param1 = pk::get_produce_param().stat;//int(pk::core["weapon_produce.stat"]);
				param2 = pk::get_produce_param().shipyard_skill; //int(pk::core["weapon_produce.shipyard_skill"]);
				break;
		}
#endif

#if not_param_set
		switch (inner_type)
		{
		case 内政类型_征兵:
			param1 = int(pk::core["recruit.stat"]);
			param2 = int(pk::core["recruit.skill"]);
			break;

		case 内政自动化_训练:
			param1 = int(pk::core["train.stat"]);
			param2 = 特技_调练;
			break;

		case 内政自动化_巡查:
			param1 = int(pk::core["inspection.stat"]);
			param2 = 特技_巡查;
			break;

		case 内政自动化_兵装:
			param1 = int(pk::core["weapon_produce.stat"]);
			param2 = int(pk::core["weapon_produce.smith_skill"]);
			break;

		case 内政自动化_育马:
			param1 = int(pk::core["weapon_produce.stat"]);
			param2 = int(pk::core["weapon_produce.stable_skill"]);
			break;

		case 内政自动化_攻具:
			param1 = int(pk::core["weapon_produce.stat"]);
			param2 = int(pk::core["weapon_produce.workshop_skill"]);
			break;

		case 内政自动化_造船:
			param1 = int(pk::core["weapon_produce.stat"]);
			param2 = int(pk::core["weapon_produce.shipyard_skill"]);
			break;
		}
#endif
		return pk::int_int(param1, param2);
	}

	/*
		获取指定武将列表内指定五维总和，最大五维，是否有某个特技
		@params actor_list {pk::list<pk::person@>} 指定武将列表
		@params stat {INT} 指定五维
		@params skill_id {INT} 指定特技
		@return first，second, third {INT_INT_BOOL} first-指定五维总和  second-执行五维最高值 third-是否有指定特技
	*/
	pk::int_int_bool get_inner_action_stat_max_skill(pk::list<pk::person@> actor_list, int stat, int skill_id)
	{
		if (actor_list.count == 0) return pk::int_int_bool(0, 0, false);

		int total = 0;
		int max = 0;
		bool has_skill = false;
		for (int i = 0; i < actor_list.count; i++)
		{
			pk::person @actor = actor_list[i];
			if (pk::is_alive(actor))
			{
				int s = actor.stat[stat];
				total = total + s;
				max = pk::max(max, s);
				if (ch::has_skill(actor, skill_id)) has_skill = true;
			}
		}

		return pk::int_int_bool(total, max, has_skill);
	}

	/*
		获取自动内政对应的优势特技跟生效五维
		@params inner_type {INT} 内政类型
		@return first，second {INT_INT} first-内政生效五维  second-优势特技
	*/
	//错误的函数，暂注释
	//pk::int_int get_person_list(pk::building @base, int type)
	//{
	//	int param1; // 排序规则1
	//	int param2; // 排序规则2
	//	switch (type)
	//	{
	//		case 内政自动化_巡查:
	//			param1 = int(pk::core["inspection.stat"]);
	//			param2 = 特技_巡查;
	//			break;

	//		case 内政自动化_训练:
	//			param1 = int(pk::core["train.stat"]);
	//			param2 = 特技_调练;
	//			break;

	//		case 内政自动化_招兵:
	//			param1 = int(pk::core["recruit.stat"]);
	//			param2 = int(pk::core["recruit.skill"]);
	//			break;

	//		case 内政自动化_兵装:
	//			param1 = int(pk::core["weapon_produce.stat"]);
	//			param2 = int(pk::core["weapon_produce.smith_skill"]);
	//			break;

	//		case 内政自动化_育马:
	//			param1 = int(pk::core["weapon_produce.stat"]);
	//			param2 = int(pk::core["weapon_produce.stable_skill"]);
	//			break;

	//		case 内政自动化_攻具:
	//			param1 = int(pk::core["weapon_produce.stat"]);
	//			param2 = int(pk::core["weapon_produce.workshop_skill"]);
	//			break;

	//		case 内政自动化_造船:
	//			param1 = int(pk::core["weapon_produce.stat"]);
	//			param2 = int(pk::core["weapon_produce.shipyard_skill"]);
	//			break;
	//	}
	//	return pk::int_int(param1, param2);
	//}

	/*
		获取自动内政执政官经验
		@params person_id {INT} 武将id
		@params type {INT} 自动内政类型
		@return  {FLOAT} 执政经验增益倍数
	*/
	float get_exp_eff(int person_id, int type)
	{
		if (!pk::is_valid_person_id(person_id)) return 1.0f;
		auto person_t = ch::get_person_p(person_id);
		array<int> exp_list = {0, 执政官经验一阶, 执政官经验二阶, 执政官经验三阶};
		array<float> exp_value = {1.0f, 1.03f, 1.05f, 1.1f};
		int exp;
		float result;
		switch(type)
		{
			case 内政自动化_巡查:
				exp = person_t.inspection_exp;
				break;
			case 内政自动化_训练:
				exp = person_t.drill_exp;
				break;
			case 内政自动化_招兵:
				exp = person_t.recruit_exp;
				break;

			case 内政自动化_兵装:
				exp = person_t.weapon_exp;
				break;

			case 内政自动化_育马:
				exp = person_t.horse_exp;
				break;

			case 内政自动化_攻具:
				exp = person_t.siege_exp;
				break;

			case 内政自动化_造船:
				exp = person_t.ship_exp;
				break;
		}
		if (exp == 0) return 1.0f;

		for (int i = 0; i < int(exp_list.length); i += 1)
		{
			if (exp >= exp_list[exp_list.length - 1])
			{
				result = exp_value[exp_value.length - 1];
				break;
			}
			if (exp >= exp_list[i] and i != int(exp_list.length - 1) and exp < exp_list[i + 1])
			{
				result = exp_value[i];
				break ;
			}
		}
		return result;
	}

	/*
		获取行动折扣值
		@params person {PERSON} 武将
		@params type {INT} 自动内政类型
		@return  {FLOAT} 折扣值
	*/
	float get_absent_eff(pk::person@ person, int type)
	{
		float result = 1.0f;

		float AI_rate = 1.0;
		//由于电脑非常频繁的把人挪来挪去,这个系数对电脑非常吃亏,削减AI的效果
		//如果未来重写了AI的移动逻辑,这里就可以优化.
		if (!person.is_player()) AI_rate = 0.3;

		if (pk::is_absent(person) or pk::is_unitize(person)) result = 1.0 - 武将不在城折扣 *  AI_rate ;
		else if (person.action_done) result = 1.0 - 武将已行动折扣 *  AI_rate ;
		
		return result;
	}
}